package com.wang.controller;

import com.wang.entity.User;
import com.wang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2024/5/31 9:40
 * @Created by pc
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 保存用户
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/save")
    public User save(@RequestBody User user) {
        return userService.saveUser(user);
    }



    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    @GetMapping(value = "/find/{username}")
    public User findUser(@PathVariable String username) {
        return userService.findByUsername(username);
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public User login(String username, String password, HttpSession session){
        User user = userService.login(username, password);
        if(null != user){
            session.setAttribute(session.getId(),user);
        }
        return user;
    }

    /**
     * 注销
     *
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute(session.getId());
        return "注销成功";
    }
}
